package com.lvmama.controller.dw;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.client.dw.RepurchaseClient;
import com.lvmama.common.enums.NewCategoryEnum;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.DateUtil;
import com.lvmama.pojo.entity.dw.RepruchaseAnalysis;

/** 复购分析
 * @author caoyong
 * @version 1.0.0
 * @since 2018-03-22 10:36
 **/
@Controller
@RequestMapping("repurchase")
@Slf4j
public class RepurchaseController {

    @Autowired
    private RepurchaseClient repurchaseClient;

    @RequestMapping("distribution")
    public String repurchaseAnalysis(Model model, HttpServletRequest request) {
    	String chooseTime = request.getParameter("chooseTime");
        log.info("repurchaseAnalysis start. param{chooseTime :" + chooseTime + "}");
        
        try {
            Map<String, Object> resultMap = this.repurchaseClient.getDistributionResult(chooseTime).getData();
            if (resultMap != null && resultMap.size() > 0){
            	 model.addAttribute("repurchaseCategories",resultMap.get("repurchaseCategories"));
            	 model.addAttribute("lastMonthUser", resultMap.get("lastMonthUser"));
            	 model.addAttribute("repurchaseCategoriesRate", resultMap.get("repurchaseCategoriesRate"));
            }
            //新品类
            model.addAttribute("categories", NewCategoryEnum.getTableShowNameEnum());
            if (!StringUtils.isEmpty(chooseTime)){
            	model.addAttribute("chooseTime", chooseTime);
            }
        } catch (Exception e) {
            log.info("repurchaseAnalysis error:." + e.getMessage(), e);
        }
        
        log.info("repurchaseAnalysis end.");
        return "/dw/repurchase/repurchaseDistribution";
    }

   

    @RequestMapping("attributes")
    public String repurchaseAttributes(Model model, HttpServletRequest request) {

	    String chooseTime = request.getParameter("chooseTime");
	    log.info("repurchaseAttributes start. param{chooseTime :" + chooseTime + "}");
	    if (!StringUtils.isEmpty(chooseTime)){
        	model.addAttribute("chooseTime", chooseTime);
        }
	    Map<String, Object> resultMap = this.repurchaseClient.getAttributesResult(chooseTime).getData();
        if (resultMap != null && resultMap.size() > 0){
        	model.addAttribute("repurchaseChart1", resultMap.get("repurchaseChart1"));
            model.addAttribute("repurchaseChart2", resultMap.get("repurchaseChart2"));
        }
        
        return "/dw/repurchase/repurchaseAttributes";
    }

    @RequestMapping("createTimeList")
    @ResponseBody
    public Result<List<String>> createTimeList(){

    	try {
    		List<String> resultList = new ArrayList<>();
            List <RepruchaseAnalysis> repruchaseAnalysisList = this.repurchaseClient.getCreateTimeList().getData();
            for(RepruchaseAnalysis repruchaseAnalysis : repruchaseAnalysisList){
                if (repruchaseAnalysis == null || repruchaseAnalysis.getCreateTime() == null){
                    continue;
                }
                resultList.add(DateUtil.dateFormat(repruchaseAnalysis.getCreateTime(), DateUtil.PATTERN_yyyy_MM_dd));
            }
            return Result.success(resultList);
		} catch (Exception e) {
			log.error(e.getMessage());
			return Result.error(CodeMessage.SERVER_ERROR);
		}
        
    }

}
